//
//  CategoryViewCell.m
//  freeLance
//
//  Created by Igor Karpenko on 3/30/13.
//  Copyright (c) 2013 Mozi Development. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "CoreDataRoutines.h"
#import "UIImage+Scale.h"

#import "CategoryViewCell.h"

@interface CategoryViewCell ()

@property (nonatomic, strong) NSString *generalImagePath;
@property (nonatomic, strong) NSString *smallImagePath1;
@property (nonatomic, strong) NSString *smallImagePath2;
@property (nonatomic, strong) NSString *smallImagePath3;

@end

@implementation CategoryViewCell

-(id) initWithCoder:(NSCoder *)aDecoder {
	self = [super initWithCoder:aDecoder];
	if (!self) {
		return nil;
	}
	
	return self;
}


-(void) awakeFromNib {
	[super awakeFromNib];
	
	if (IS_IPAD) {
		UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ctg_cell_background"]];
		[self.contentBackgroundView addSubview:backgroundImageView];
		[self.contentBackgroundView sendSubviewToBack:backgroundImageView];
	} 
	
	UIColor *borderColor = RGB(189.0f, 195.0f, 199.0f);
	self.generalImageView.layer.borderWidth = 1.0f;
	self.generalImageView.layer.borderColor = [borderColor CGColor];
	
	self.smallImageView1.layer.borderWidth = 1.0f;
	self.smallImageView1.layer.borderColor = [borderColor CGColor];
	self.smallImageView2.layer.borderWidth = 1.0f;
	self.smallImageView2.layer.borderColor = [borderColor CGColor];
	self.smallImageView3.layer.borderWidth = 1.0f;
	self.smallImageView3.layer.borderColor = [borderColor CGColor];
}


-(void) setCollection:(CDCollections *)collection {
    self.generalImageView.image = nil;
    self.smallImageView1.image = nil;
    self.smallImageView2.image = nil;
    self.smallImageView3.image = nil;
    
	_collection = collection;
    if (!_collection) {
        return ;
    }
	
	_titleLabel.text = _collection.name;
	
    self.generalImagePath = [self imagePathForColection:NO];
	self.smallImagePath1 = [self imagePathForColection:YES];
	self.smallImagePath2 = [self imagePathForColection:YES];
	self.smallImagePath3 = [self imagePathForColection:YES];
	
	dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^() {
        NSString *generalImagePathCopy = [self.generalImagePath copy];
        NSString *smallImagePath1Copy = [self.smallImagePath1 copy];
        NSString *smallImagePath2Copy = [self.smallImagePath2 copy];
        NSString *smallImagePath3Copy = [self.smallImagePath3 copy];
        
		NSData *imageData = [NSData dataWithContentsOfFile:generalImagePathCopy];
		UIImage *generalImage = [UIImage imageWithData:imageData];

		imageData = [NSData dataWithContentsOfFile:smallImagePath1Copy];
		UIImage *smallImage1 = [UIImage imageWithData:imageData];
		
		imageData = [NSData dataWithContentsOfFile:smallImagePath2Copy];
		UIImage *smallImage2 = [UIImage imageWithData:imageData];
		
		imageData = [NSData dataWithContentsOfFile:smallImagePath3Copy];
		UIImage *smallImage3 = [UIImage imageWithData:imageData];
		dispatch_async(dispatch_get_main_queue(), ^() {
            if ([generalImagePathCopy isEqualToString:self.generalImagePath])
                self.generalImageView.image = generalImage;
            if ([smallImagePath1Copy isEqualToString:self.smallImagePath1])
                self.smallImageView1.image = smallImage1;
            if ([smallImagePath2Copy isEqualToString:self.smallImagePath2])
                self.smallImageView2.image = smallImage2;
            if ([smallImagePath3Copy isEqualToString:self.smallImagePath3])
                self.smallImageView3.image = smallImage3;
        });
	});
}


#pragma mark - Private methods

-(NSString *) imagePathForColection:(BOOL)isImageHeight60 {
	NSArray *products = [_collection.products  allObjects];
    if (!products.count) {
        return nil;
    }
	int index = arc4random() % products.count;
	CDProducts *product = [products objectAtIndex:index];
	
	NSArray *images = [product.images allObjects];
    for (CDImages *image in images) {
        if ([[[[image.url stringByDeletingPathExtension] lastPathComponent] componentsSeparatedByString:@"_"] count] == 2) {
            return isImageHeight60 ? image.imagePathThumbnail60 : image.imagePathThumbnail200;
        }
    }
    
	index = arc4random() % images.count;
	CDImages *image = [images objectAtIndex:index];
    
    
	return isImageHeight60 ? image.imagePathThumbnail60 : image.imagePathThumbnail200;
}


@end